import axios from 'axios';
import { showLoadingToast } from 'vant';

const SERVER = 'http://localhost:5000'

export default function post(api, params, isWait = true) {
  return new Promise(resolve => {
    const toast = isWait && showLoadingToast({
      message: '加载中...',
      forbidClick: true,
      duration: 0,
      loadingType: 'spinner',
    });
    console.log(`[${api}][send]:`, params);
    axios.post(`${SERVER}${api}`, params).then(res => {
      console.log(`[${api}][recv]:`, res.data);
      resolve(res.data);
      toast && toast.close();
    }).catch(e => {
      console.log(`[${api}][error]:`, e.message);
      resolve({ message: e.message });
      toast && toast.close();
    });
  });
}